<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {

	##餐币充值
    public function cz(){
		//判断用户是否登陆
		 if(session('username')!=null)
		 {	
		 	//收集用户信息
		 	$username = session('username');
		 	$userid	  = session('userid');

		 	//实例化用户model 打印用户信息
		 	$userModel = D('User');
		 	$info = $userModel->alias('u')->join('LEFT JOIN __RECHARGE__ r on u.uid=r.uid')->order('pubtime desc')->limit(1)->field('u.*,r.pubtime,r.cmoney')->where(array('u.uid'=>$userid))->find();
		 	$this->assign('info',$info);
			$this->display();
		 }
		 else
		 {
		 	$this->redirect('login');
		 }
	
	}

	##订单查询
	public function ddcx(){
		
		
		$this->display();
	}


	##个人资料
	public function grzl(){
		
		//判断用户是否登陆
		if(session('username')!=null)
		{	
			//获取用户信息   id/name 
			$username = session('username');
			$userid   = session('userid');	
			
			//实例化用户对象 打印出相应数据
			$userModel = D('User');
			$info = $userModel->where(array('uid'=>$userid,'name'=>$username))->find();
			
			$this->assign('info',$info);
			$this->display();
		}
		else
		{	
			//验证失败  跳转到登陆页面
			$this->redirect('login');
		}
		
	}


	##积分充值
	public function jfcx(){
		//判断用户是否登陆
		if(session('username')!=null)
		{
			//收集用户信息
			$username = session('username');
			$userid   =	session('userid');

			//实例化对象 查询用户信息
			$userModel = D('User');
			$info = $userModel->where(array('name'=>$username,'uid'=>$userid))->find();
			$this->assign('info',$info);

			$this->display();
		}
		else
		{
			$this->redirect('login');
		}	
		
	}

	##奖品礼品
	public function jplp(){
		//判断用户是否登陆
		 if(session('username')!=null)
		 {	
		 	//收集用户信息
		 	$username = session('username');
		 	$userid	  = session('userid');

		 	//实例化用户model
		 	$userModel = D('User');
		 	$info = $userModel->where(array('name'=>$username,'uid'=>$userid))->find();
		 	$this->assign('info',$info);
			$this->display();
		 }
		 else
		 {
		 	$this->redirect('login');
		 }
		
	
	}


	##修改密码
	public function xgmm(){
		if(IS_POST)
		{
			//通过session 接收用户ID信息  //检测旧密码
			$userid = session('userid');	
			$username = session('username');
			$oldPwd = $_POST['oldpwd'];
			
			//判断旧密码是否正确
			$userModel = D('User');
			$uinfo = $userModel->where(array('uid'=>$userid,'password'=>$oldPwd,'name'=>$username))->find();

			//接收新的密码信息 update到数据库
			if($uinfo!==null){
				
				//收集信息	
				$newPD = $_POST['newpwd'];
				$relPwd = $_POST['ralpwd'];
				if($newPD!==$relPwd)
				{	
					$this->error('密码不一致','xgmm',array('uid'=>$userid),2);					
				}
				else
				{	
					$res = $userModel->where(array('uid'=>$userid,'name'=>$username))->save(array('password'=>$newPD));
					if($res)
					{	
						$this->success('修改成功,请重新登录','logout',2);
					}
					else
					{
						$this->error('修改失败','xgmm',2);
					}
					
					
				}

			}	
		}	
		else
		{
			$this->display();
		}
		
		
	}

	##显示历史充值记录
	public function showHistory()
	{	
		$uid = I('get.uid');
		$userModel = D('User');

		//分页类
		$total = D('Recharge')->where(array('uid'=>$uid))->count();  ##数据总量
		$per = 1;
		$page = new \Common\Tools\Page($total,$per);

		//所有信息
		$ruinfo = $userModel->alias('u')->join('LEFT JOIN __RECHARGE__ r on r.uid=u.uid')->field('r.*,u.name')->order('pubtime desc')->limit($page->offset,$per)->where(array('r.uid'=>$uid))->select();
		
		//分页信息
		$plist = $page->fpage(array(3,4,5,6,7,8));


		$this->assign('plist',$plist);
		$this->assign('ruinfo',$ruinfo);
		$this->display();
	}


	##积分充值
	public function jfcz(){
		//判断用户是否登陆
		if(session('username')!=null)
		{	
			//获取用户信息   id/name 
			$username = session('username');
			$userid   = session('userid');	
			
			//实例化用户对象 打印出相应数据
			$userModel = D('User');
			$info = $userModel->where(array('uid'=>$userid,'name'=>$username))->find();
			
			$this->assign('info',$info);
			$this->display();
		}
		else
		{	
			//验证失败  跳转到登陆页面
			$this->redirect('login');
		}
		
		$this->display();
	}


	##验证码
	public function code(){
		$cfg = array('length'=>4);
		$code = new \Think\Verify($cfg);
		$code->entry();
	}


	##用户登陆
	public function login(){
			if(IS_POST)
			{
				//array(2) { ["name"]=> string(0) "" ["password"]=> string(0) "" }
				//检测用户信息的合法性
				$usrModel = D('User');
				$usr = $_POST['name'];
				$pwd = $_POST['password'];
				$info = $usrModel->where(array('name'=>$usr,'password'=>$pwd))->find();

				if($info!=null)
				{
					//检测合格后 session持久化操作, name/id 
					session('username',$info['name']);
					session('userid',$info['uid']);

					//跳转到个人中心
					$this->redirect('home/user/grzl');

				}
				else
				{
					//检测有误 跳转回登陆页面
					 $this->error('账号或密码错误','login',2);
				}

			}
			else
			{
				$this->display();
			}		
	}

	##用户退出
	public function logout(){

		//清除session信息
		Session(null);


		//跳转到登陆页面
		$this->redirect('login');
	}

	##用户注册
	public function register(){
		$usr = D('User');
		$yzm = I('yzm');
		$name = I('post.name');	
		$Verify = new \Think\Verify();


		//现示页面    
		if(!IS_POST)
		{
			$this->display();
		}
		else
		{
			//发送注册数据  		
			if(!$usr->create())
			{
				$usr->getError();
				$this->error();
			}
			else
			{		
				if(!$Verify->check($yzm))
				{
					
					$this->error('验证码错误'); //验证未通过,重新跳转到注册页
				}
				else
				{	
					$usr->add();
					$uid = D('User')->field('uid')->order('uid desc')->limit(1)->select();
					$this->success('恭喜注册成功','login',2 );
				}
				
			}
		
		}		
		
	}

}